#include <QtOpenGL>

#include "glWidgetDefault.h"

/****************************************************//**
 * Namespace des Projektes
 ********************************************************/
namespace jae
{

/****************************************************//**
 * Constructor of the class.
 * @param aParent parentwidget
 ********************************************************/
GLWidgetDefault::GLWidgetDefault(SceneManager *aSceneManager, QGLWidget* aParent):
  GLWidget(aSceneManager, aParent)
{         
    //TODO Camera anpassen
    _pCamera = aSceneManager->createCamera();
    
    _pressedControl = false;
    _pressedShift = false;
    _pressedW = false;
    _pressedS = false;
    _pressedA = false;
    _pressedD = false;
    
    //Add a Quit-Button
    _pQuitButton = new QPushButton("Quit", this);
    _pQuitButton->resize(100, 40);
    _pQuitButton->move(this->width() - 110, 10);
    _pQuitButton->setFont(QFont("Times", 18, QFont::Bold));
    _pQuitButton->setFocusPolicy(Qt::NoFocus);

    //Connect the clicked signal with the quit() method
    QObject::connect(_pQuitButton, SIGNAL(clicked()), this, SLOT(close()));

    //Show Button
    _pQuitButton->show();  
    
    //Set Camera
    this->getCamera()->moveBackward(-8.0f);
    this->getCamera()->moveUpward(0.5f);
    
    this->activateWindow(); //necessary to get Keyboardinputs 
}

/****************************************************//**
 * Destructor of the class.
 ********************************************************/
GLWidgetDefault::~GLWidgetDefault()
{
    delete _pQuitButton; _pQuitButton = NULL;
}

/****************************************************//**
 * This method is called whenever the widget has been
 * resized.
 * @param aEvent the ResizeEvent-object
 ********************************************************/
void GLWidgetDefault::resizeEvent(QResizeEvent * aEvent)
{   
    GLWidget::resizeEvent(aEvent);
    
    _pQuitButton->move(this->width() - 110, 10);
}

/****************************************************//**
 * This function is called whenever the mouse has been 
 * pressed.
 * @param aEvent MouseButton-Event
 ********************************************************/
void GLWidgetDefault::mousePressEvent(QMouseEvent *aEvent)
{
    _lastMousePos = aEvent->pos();
}

/****************************************************//**
 * The function is used to rotate the 3D objects.
 * @param aEvent MouseButton-Event
 ********************************************************/
void GLWidgetDefault::mouseMoveEvent(QMouseEvent *aEvent)
{
    GLfloat dx = _lastMousePos.x() - aEvent->x();
    GLfloat dy = _lastMousePos.y() - aEvent->y();

    if (aEvent->buttons() & Qt::RightButton)
      {
        this->getCamera()->moveForward(-dy / 10.0f);
        this->getCamera()->roll(-dx / 4.0f);
      }
    else if (aEvent->buttons() & Qt::LeftButton)
          {
        this->getCamera()->pitch(-dy / 4.0f);
        this->getCamera()->yaw(-dx / 4.0f);
          }
    _lastMousePos = aEvent->pos();
}

/****************************************************//**
 * This method is called when a key is pressed
 * @param aEvent KeyPress-Event
 ********************************************************/
void GLWidgetDefault::keyPressEvent (QKeyEvent* aEvent)
{ 
    switch(aEvent->key())
    {
    case Qt::Key_W:
        _pressedW = true;
        break;
    case Qt::Key_S:
        _pressedS = true;
        break;
    case Qt::Key_A:
        _pressedA = true;
        break;
    case Qt::Key_D:
        _pressedD = true;
        break;
    case Qt::Key_Control:
        _pressedControl = true;
        break;
    case Qt::Key_Shift:
        _pressedShift = true;   
        break;
    }//switch Key
}

/****************************************************//**
 * This method is called when a key is released
 * @param aEvent KeyPress-Event
 ********************************************************/
void GLWidgetDefault::keyReleaseEvent (QKeyEvent* aEvent)
{
    switch(aEvent->key())
    {
    case Qt::Key_W:
        _pressedW = false;
        break;
    case Qt::Key_S:
        _pressedS = false;
        break;
    case Qt::Key_A:
        _pressedA = false;
        break;
    case Qt::Key_D:
        _pressedD = false;
        break;
    case Qt::Key_Control:
        _pressedControl = false;
        break;
    case Qt::Key_Shift:
        _pressedShift = false;   
        break;
    }//switch Key
}

/****************************************************//**
 * This method is called whenever there is a redrawing.
 * @param aEvent KeyPaint-Event
 ********************************************************/
void GLWidgetDefault::paintGLEvent ()
{
    //Set Camera Movement

    if (_pressedControl)
    {
        if(_pressedW)
            this->getCamera()->pitch(-1.0f);
        if(_pressedS)
            this->getCamera()->pitch( 1.0f);
        if(_pressedA)
            this->getCamera()->yaw(-1.0f);
        if(_pressedD)
            this->getCamera()->yaw( 1.0f);
    }
    else if(_pressedShift)
    {
        if(_pressedW)
            this->getCamera()->moveUpward( 0.1f);
        if(_pressedS)
            this->getCamera()->moveUpward(-0.1f);
        if(_pressedA)
            this->getCamera()->moveLeft(-0.1f);
        if(_pressedD)
            this->getCamera()->moveLeft( 0.1f);
        
    }
    else
    {
        if(_pressedW)
            this->getCamera()->moveForward(-0.1f);
        if(_pressedS)
            this->getCamera()->moveForward( 0.1f);
        if(_pressedA)
            this->getCamera()->moveLeft(-0.1f);
        if(_pressedD)
            this->getCamera()->moveLeft( 0.1f);
    }
}

} //namespace jae
